package com.example.musicwebcon.service;

import com.example.musicwebcon.model.dto.recentDTO;
import com.example.musicwebcon.model.recentSong;
import com.example.musicwebcon.repository.recentRepository;
import com.example.musicwebcon.service.serviceInterface.IrecentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class recentService implements IrecentService {
    @Autowired
    recentRepository recentRepository;
    @Override
    public recentSong add(recentDTO recentSong) {
        recentSong recentsong = new recentSong();
        BeanUtils.copyProperties(recentSong,recentsong);
        return recentRepository.save(recentsong);
    }

    @Override
    public recentSong getMusicId(String musicId) {
        return recentRepository.findById(musicId).orElseThrow(() -> {
            throw new IllegalArgumentException("歌曲不存在，参数异常！");
        });
    }

    @Override
    public Iterable<recentSong> getALl() {
        return recentRepository.findAll();
    }

    @Override
    public recentSong edit(recentDTO recentSong) {
        recentSong recentsong = new recentSong();
        BeanUtils.copyProperties(recentSong,recentsong);
        return recentRepository.save(recentsong);
    }

    @Override
    public void deleteMusicId(String musicId) {
        recentRepository.deleteById(musicId);
    }
}
